package org.peterbjornx.landscape;

import org.peterbjornx.io.AbstractFileStore;
import org.peterbjornx.io.RSInputStream;

import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: PeterbjornX
 * Date: Apr 9, 2009
 * Time: 7:37:33 PM
 * Gets the data index for a set of coordinates
 */
public class LandscapeIndex {
    private static int[] mapindices4;
    private static int[] objectindexes;
    private static int[] landindexes;
    private static int[] coordinates;

    public static void initializeIndexTable(AbstractFileStore indexstore){
        try {
            RSInputStream mapidx = indexstore.getFile("map_index");
            int idxcount = mapidx.size()/7;
            coordinates = new int[idxcount];
            landindexes = new int[idxcount];
            objectindexes = new int[idxcount];
            mapindices4 = new int[idxcount];
            for (int i = 0;i < idxcount;i++){
                coordinates[i] = mapidx.readUShort();
                landindexes[i] = mapidx.readUShort();
                objectindexes[i] = mapidx.readUShort();
                mapindices4[i] = mapidx.read();
            }
        } catch (IOException e) {
            System.err.println("[IndexLookupSystem] SEVERE: Exception caught during landscape index table initialization.");
        }
    }

    public static int getMapIndex(boolean obj,int x, int y){
        int coordinateshort = (x << 8) + y;
        for (int i =0;i< coordinates.length;i++)
            if (coordinates[i] == coordinateshort)
                if (obj)
                    return objectindexes[i];
                else
                    return landindexes[i];
        return -1;
    }

}
